<?php
namespace app\shop\controller;
use app\rbac\model\AdminM;
use app\shop\model\GoodCatM;
use think\Db;
use think\Session;
use app\base\controller\Base;

class Goodcat extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增
     * @return mixed
     */
    public function add()
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            $this->error('权限不够');
        }
        $model = new GoodCatM();
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $res = $model->add($data);
            if($res)
            {
                $this->success('新增成功',url('shop/Goodcat/lst'));
            }
            else
            {
                $this->error($model->msg);
            }
        }
        $catData = $model->getCatTree();
        $this->assign(array(
            '_page_title'=>'添加分类',
            '_page_title_english'=>'Category add',
            '_btn_title'=>'分类列表',
            '_btn_link'=>'lst',
            '_page_ico'=>'iconfa-group',
            'catData'=>$catData,
        ));
        return $this->fetch();
    }



    /**
     * 编辑页面
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            $this->error('权限不够');
        }
        $model = new GoodCatM();
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $res = $model->edit($data);
            if($res)
            {
                $this->success('修改成功',url('shop/Goodcat/lst'));
            }
            else
            {
                $this->error($model->msg);
            }
        }
        $catData = $model->getCatTree();
        $data = Db::table('shop_goods_cat')->find($id);
        $this->assign(array(
            '_page_title'=>'修改分类',
            '_page_title_english'=>'Category edit',
            '_btn_title'=>'分类列表',
            '_btn_link'=>url('lst'),
            '_page_ico'=>'iconfa-group',
            'catData'=>$catData,
            'data'=>$data,
        ));
        return $this->fetch();
    }

    /**
     * 获取全部数据
     * @return mixed
     */
    public function lst()
    {
        $model = new GoodCatM();
        $data = $model->lst();
        $this->assign(array(
            '_page_title'=>'分类列表',
            '_page_title_english'=>'Category list',
            '_btn_title'=>'添加分类',
            '_btn_link'=>url('add'),
            '_page_ico'=>'iconfa-group',
            'data'=>$data,
        ));
        return $this->fetch();
    }


    /**
     * 删除数据
     * @param $id
     */
//    public function del()
//    {
//        //先判断权限
//        $admin_id = session('admin_id');
//        $adminModel = new AdminM();
//        $res = $adminModel->isAdmin($admin_id);
//        if($res === false)
//        {
//            return json(['code'=>-1,'msg'=>'没有权限']);
//        }
//        $id = $this->request->post('id');
//        $model = new GoodCatM();
//        $model->del($id);
//        return json(['code'=>$model->code,'msg'=>$model->msg]);
//    }
}
